package com.cmc.cloud.cmclink.doc.util.so;


import io.swagger.v3.oas.annotations.media.Schema;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;

/**
 * 国际化自动转换类
 *
 * @author chenzheng
 * @date 2025/08/31 11:25
 */
public class GlobalizationUtil {

    private static Set<String> keySet = new HashSet<>(1000);

    public static void main(String[] args) throws Exception {
        String[] packageList = new String[]{"com.cmc.cloud.cmclink.doc.vo.multimodaltransport.containermanifest",
            "com.cmc.cloud.cmclink.doc.vo.multimodaltransport.showexportfreight"};
        for (String packageName : packageList) {
            globalizationPackage(packageName);
        }
    }

    public static void globalizationPackage(String packageName) throws Exception {
        //String packageName = "com.cmc.cloud.cmclink.doc.vo.manifest";
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        Package packagePath = Package.getPackage(packageName);
        URL packageUrl = classLoader.getResource(packageName.replace(".", "/"));
        if (packageUrl != null) {
            func(packageName, new File(packageUrl.toURI()));
        }
    }

    private static void func(String packageName, File file) throws Exception {
        File[] fs = file.listFiles();
        for (File f : fs) {
            if (f.isDirectory()) {
                // 若是目录，则递归目录下的文件
                func(packageName + "." + f.getName(), f);
            }
            if (f.isFile()) {
                tanslate(packageName, f);
            }
        }
    }

    private static final String CLASS_FILE_SUFFIX = ".class";

    public static void tanslate(String packageName, File file) throws Exception {
        if (file.getName().endsWith(CLASS_FILE_SUFFIX)) {
            String classN = file.getName().replace(CLASS_FILE_SUFFIX, "");
            String className = packageName + "." + classN;
            Class clazz = Class.forName(className);
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                Schema schema = field.getAnnotation(Schema.class);
                NotNull notNull = field.getAnnotation(NotNull.class);
                NotBlank notBlank = field.getAnnotation(NotBlank.class);
                Length length = field.getAnnotation(Length.class);
                String description = "";
                if (schema != null) {
                    description = schema.description();
                }
                classN = classN.replaceAll("ReqVO", "").replaceAll("BaseVO", "")
                        .replaceAll("Update", "").replaceAll("Create", "");


                if (notNull != null) {
                    String name = (classN + "." + field.getName() + "." + "NotNull").toLowerCase();
                    if (keySet.contains(name) == false) {
                        keySet.add(name);
                        System.out.println(name + "\t" + (description + "不能为空").replace(" ", ""));
                    }
                }
                if (notBlank != null) {
                    String name = (classN + "." + field.getName() + "." + "notBlank").toLowerCase();
                    if (keySet.contains(name) == false) {
                        keySet.add(name);
                        System.out.println(name + "\t" + (description + "不能为空").replace(" ", ""));
                    }
                }
                if (length != null) {
                    String name = (classN + "." + field.getName() + "." + "length").toLowerCase();
                    if (keySet.contains(name) == false) {
                        keySet.add(name);
                        if (length.min() != 0) {
                            System.out.println(name + "\t" + (description + "支持最小长度" + length.min()).replace(" ", ""));
                        }
                        if (length.max() != 0) {
                            System.out.println(name + "\t" + (description + "支持最大长度" + length.max()).replace(" ", ""));
                        }
                    }
                }
            }
        }
    }
}
